﻿class ColorChannelSliderControl{
	
	private var __instance:MovieClip;
	private var __width:Number;
	private var __height:Number;
	private var __color:Number;
	
	private var __border:MovieClip;
	
	private var __bar:MovieClip;
	private var __barWidth:Number;
	private var __barHeight:Number;
	
	private var __value:Number;
	
	function ColorChannelSliderControl(instance_mc:MovieClip, w:Number, h:Number, c:Number){
		__instance = instance_mc;
		__width = w;
		__height = h;
		__color = c;
		
		__barWidth = __width / 50;
		__barHeight = __height;
		
		init();
		
		this.value = 100;
	}
	
	private function init():Void{
		drawBG();
		drawBar();
		drawBorder(0x000000);
	}
	
	private function drawBG():Void{
		__instance.beginGradientFill("linear", [0xffffff, __color], [20, 90], [0, 127]
									, {matrixType:"box", x:-50, y:-50, w:__width, h:__height, r:0});
		__instance.moveTo(-__width/2, -__height/2);
		__instance.lineTo(__width/2, -__height/2);
		__instance.lineTo(__width/2, __height/2);
		__instance.lineTo(-__width/2, __height/2);
		__instance.lineTo(-__width/2, -__height/2);
		__instance.endFill();
	}
	
	private function drawBorder(c:Number):Void{
		if(__border!=null){
			__border.removeMovieClip();
			__border = null;
		}
		__border = __instance.createEmptyMovieClip("border_mc", 3);
		__border.lineStyle(1, c, 100);
		var hw:Number = Math.round(__width/2);
		var hh:Number = Math.round(__height/2);
		__border.moveTo(-hw, -hh);
		__border.lineTo(hw, -hh);
		__border.lineTo(hw, hh);
		__border.lineTo(-hw, hh);
		__border.lineTo(-hw, -hh);
	}
	
	private function drawBar():Void{
		__bar = __instance.createEmptyMovieClip("bar_mc", 2);
		__bar.lineStyle(0, 0x000000, 100);
		__bar.beginFill(0x000000, 100);
		var hw:Number = Math.round(__barWidth/2);
		var hh:Number = Math.round(__barHeight/2);
		__bar.moveTo(-hw, -hh);
		__bar.lineTo(hw, -hh);
		__bar.lineTo(hw, hh);
		__bar.lineTo(-hw, hh);
		__bar.lineTo(-hw, -hh);
		__bar.endFill();
	}
	
	private function redraw():Void{
		__bar._x = ((__value / 100) - 0.5) * (__width - __barWidth);
	}
	
	public function set value(v:Number):Void{
		if(v<0)v=0;
		if(v>100)v=100;
		
		__value = v;
		redraw();
	}
	
	public function get value():Number{
		return __value;
	}
	
	public function setFocus(tf:Boolean):Void{
		if(tf){
			drawBorder(0xffcc00);
		}else{
			drawBorder(0x000000);
		}
	}
}